import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        //使用数组 二分法查找输入的数字是否存在
        checkInputNum();
        //设置一个汉字,传入方法中，生成编码
        toUnicode("你好");
        //title : 写一段程序，体现byte,int,float,double之间的类型转换
        typeConversion();
        //计算器，入口
        calculator();
        //写一段程序，练习定义与使用数组。打印数组元素个数，实现数组赋值操作，打印数组元素。
        arrayDemo();
        //编写一个应用程序，给出汉字“你”“我”“他”在Unicode表中的位置。
        char[] characters = {'你', '我', '他'};
        charToUnicode(characters);
        //编写一个java应用程序，输出全部的希腊字母。
        getGreekAlphabet();


    }

    private static void checkInputNum() {
        Scanner scanner = new Scanner(System.in);
        int[] a = {12, 34, 9, 23, 45, 6, 45, 90, 123, 19, 34};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        System.out.println("输入整数，程序判断该整数是否在数组中：");
        int number = scanner.nextInt();
        int index = Arrays.binarySearch(a, number);//二分搜索法（折半查找）
        if (index >= 0) System.out.println(number + "和数组中索引为" + index + "的元素值相同");
        else System.out.println(number + "不与数组中任何元素值相同");

    }

    private static void toUnicode(String chineseCharacter) {

        int codePoint = chineseCharacter.codePointAt(0);  // 获取Unicode码
        String unicode = Integer.toHexString(codePoint).toUpperCase();  // 将Unicode码转成十六进制字符串
        System.out.println("汉字位置：" + codePoint);//汉字位置
        System.out.println("汉字为：" + chineseCharacter + "  Unicode的编码为：" + unicode);

    }

    private static void typeConversion() {

        int intValue = 0;
        float floatValue = 0.0f;
        double doubleValue = 0.0;
        byte byteValue = 10;

        // byte转换Int
        intValue = byteValue;
        System.out.println("byte转换Int: " + intValue);

        // int转换float
        floatValue = (float) intValue;
        System.out.println("int转换float: " + floatValue);

        // float转换double
        doubleValue = (double) floatValue;
        System.out.println("float转换double: " + doubleValue);

        // double转换float
        floatValue = (float) doubleValue;
        System.out.println("double转换float: " + floatValue);

        // float转换Int
        intValue = (int) floatValue;
        System.out.println("float转换Int: " + intValue);

        // int转换Byte
        byteValue = (byte) intValue;
        System.out.println("int转换Byte: " + byteValue);
    }


    private static void calculator() {

        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入第一个数：");
        BigDecimal num1 = scanner.nextBigDecimal();

        System.out.print("请输入第二个数：");
        BigDecimal num2 = scanner.nextBigDecimal();

        System.out.print("请选择运算符（+、-、*、/）：");
        char alculationSymbol = scanner.next().charAt(0);

        BigDecimal result = BigDecimal.ZERO;

        switch (alculationSymbol) {
            case '+':
                result = num1.add(num2);
                break;
            case '-':
                result = num1.subtract(num2);
                break;
            case '*':
                result = num1.multiply(num2);
                break;
            case '/':
                if (num2.compareTo(BigDecimal.ZERO) != 0) {
                    result = num1.divide(num2, 10, BigDecimal.ROUND_HALF_UP);
                } else {
                    System.out.println("除数不能为零");
                    return;
                }
                break;
            default:
                System.out.println("运算符无效");
                return;
        }
        System.out.println("计算结果为：" + result.toString());
    }

    private static void arrayDemo() {
        int[] numbers = new int[3];


        System.out.println("数组长度：" + numbers.length);

        //赋值数组参数
        for (int i = 0; i < 3; i++) {
            numbers[i] = i + 1;
        }


        System.out.println("数组元素：");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }


    private static void charToUnicode(char[] characters) {
        for (char ch : characters) {
            int codePoint = ch;
            String hex = Integer.toHexString(codePoint).toUpperCase();
            System.out.println("\"" + ch + "\"" + " 的Unicode表示为：");
            System.out.println("十进制：" + codePoint + " 十六进制：" + hex);
        }
    }


    private static void getGreekAlphabet() {
        char alpha = '\u03B1';

        System.out.println("希腊字母表：");
        for (int i = 0; i < 24; i++) {
            System.out.print(alpha++ + " ");
        }
    }


}






